home *** CD-ROM | disk | FTP | other *** search
/ Sprite 1984 - 1993 / Sprite 1984 - 1993.iso / man / lib.fmt / c / getopt.man < prev    next >
Encoding:
Text File  |  1989-04-20  |  4.6 KB  |  199 lines

  1.  
  2.  
  3.  
  4. GETOPT                C Library Procedures                 GETOPT
  5.  
  6.  
  7.  
  8. NNAAMMEE
  9.      getopt - get option letter from argv
  10.  
  11. SSYYNNOOPPSSIISS
  12.      iinntt ggeettoopptt((aarrggcc,, aarrggvv,, ooppttssttrriinngg))
  13.      iinntt aarrggcc;;
  14.      cchhaarr ****aarrggvv;;
  15.      cchhaarr **ooppttssttrriinngg;;
  16.  
  17.      eexxtteerrnn cchhaarr **ooppttaarrgg;;
  18.      eexxtteerrnn iinntt ooppttiinndd;;
  19.      eexxtteerrnn iinntt oopptteerrrr;;
  20.  
  21. DDEESSCCRRIIPPTTIIOONN
  22.      _G_e_t_o_p_t returns the next option letter in _a_r_g_v that matches a
  23.      letter in _o_p_t_s_t_r_i_n_g.  _O_p_t_s_t_r_i_n_g is a string of recognized
  24.      option letters; if a letter is followed by a colon, the
  25.      option is expected to have an argument that may or may not
  26.      be separated from it by white space.  _O_p_t_a_r_g is set to point
  27.      to the start of the option argument on return from _g_e_t_o_p_t.
  28.  
  29.      _G_e_t_o_p_t places in _o_p_t_i_n_d the _a_r_g_v index of the next argument
  30.      to be processed.  Because _o_p_t_i_n_d is external, it is normally
  31.      initialized to zero automatically before the first call to
  32.      _g_e_t_o_p_t.
  33.  
  34.      When all options have been processed (i.e., up to the first
  35.      non-option argument), _g_e_t_o_p_t returns EEOOFF.  The special
  36.      option ---- may be used to delimit the end of the options; EEOOFF
  37.      will be returned, and ---- will be skipped.
  38.  
  39. DDIIAAGGNNOOSSTTIICCSS
  40.      _G_e_t_o_p_t prints an error message on _s_t_d_e_r_r and returns a ques-
  41.      tion mark (??) when it encounters an option letter not
  42.      included in _o_p_t_s_t_r_i_n_g.  Setting _o_p_t_e_r_r to a zero will dis-
  43.      able this error message.
  44.  
  45. EEXXAAMMPPLLEE
  46.      The following code fragment shows how one might process the
  47.      arguments for a command that can take the mutually exclusive
  48.      options aa and bb, and the options ff and oo, both of which
  49.      require arguments:
  50.  
  51.           main(argc, argv)
  52.           int argc;
  53.           char **argv;
  54.           {
  55.                int c;
  56.                extern int optind;
  57.                extern char *optarg;
  58.                .
  59.                .
  60.  
  61.  
  62.  
  63. Sprite v1.0                May 6, 1988                          1
  64.  
  65.  
  66.  
  67.  
  68.  
  69.  
  70. GETOPT                C Library Procedures                 GETOPT
  71.  
  72.  
  73.  
  74.                .
  75.                while ((c = getopt(argc, argv, "abf:o:")) != EOF)
  76.                     switch (c) {
  77.                     case `a':
  78.                          if (bflg)
  79.                               errflg++;
  80.                          else
  81.                               aflg++;
  82.                          break;
  83.                     case `b':
  84.                          if (aflg)
  85.                               errflg++;
  86.                          else
  87.                               bproc();
  88.                          break;
  89.                     case `f':
  90.                          ifile = optarg;
  91.                          break;
  92.                     case `o':
  93.                          ofile = optarg;
  94.                          break;
  95.                     case `?':
  96.                     default:
  97.                          errflg++;
  98.                          break;
  99.                     }
  100.                if (errflg) {
  101.                     fprintf(stderr, "Usage: ...");
  102.                     exit(2);
  103.                }
  104.                for (; optind < argc; optind++) {
  105.                     .
  106.                     .
  107.                     .
  108.                }
  109.                .
  110.                .
  111.                .
  112.           }
  113.  
  114. HHIISSTTOORRYY
  115.      Written by Henry Spencer, working from a Bell Labs manual
  116.      page.  Modified by Keith Bostic to behave more like the Sys-
  117.      tem V version.
  118.  
  119. BBUUGGSS
  120.      ``-'' may be specified as an option letter, however it
  121.      should never have an argument associated with it.  This
  122.      allows getopt to be used with programs that think that ``-''
  123.      means standard input.
  124.  
  125.      Option arguments are allowed to begin with ``-''; this is
  126.  
  127.  
  128.  
  129. Sprite v1.0                May 6, 1988                          2
  130.  
  131.  
  132.  
  133.  
  134.  
  135.  
  136. GETOPT                C Library Procedures                 GETOPT
  137.  
  138.  
  139.  
  140.      reasonable but reduces the amount of error checking possi-
  141.      ble.
  142.  
  143.      _G_e_t_o_p_t is quite flexible but the obvious price must be paid:
  144.      there is much it could do that it doesn't, like checking
  145.      mutually exclusive options, checking type of option argu-
  146.      ments, etc.
  147.  
  148.  
  149.  
  150.  
  151.  
  152.  
  153.  
  154.  
  155.  
  156.  
  157.  
  158.  
  159.  
  160.  
  161.  
  162.  
  163.  
  164.  
  165.  
  166.  
  167.  
  168.  
  169.  
  170.  
  171.  
  172.  
  173.  
  174.  
  175.  
  176.  
  177.  
  178.  
  179.  
  180.  
  181.  
  182.  
  183.  
  184.  
  185.  
  186.  
  187.  
  188.  
  189.  
  190.  
  191.  
  192.  
  193.  
  194.  
  195. Sprite v1.0                May 6, 1988                          3
  196.  
  197.  
  198.  
  199.